<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
// use App\Traits\HasDateTimeFormatter;
use Dcat\Admin\Traits\HasDateTimeFormatter;

class HhmOrder extends BaseModel
{
    use HasDateTimeFormatter;
    
    protected $table = 'ims_hhm_order';
    protected $guarded = [''];
    
    protected $appends = ['status_name', 'pay_type_name'];
    
    // 订单状态常量
    const CLOSE = -1;
    const WAIT_PAY = 0;
    const WAIT_SEND = 1;
    const WAIT_RECEIVE = 2;
    const COMPLETE = 3;
    const REFUND = 11;

    // 禁用Laravel的自动时间戳管理
    public $timestamps = true;


        // 添加缺失的字段到 fillable 数组
        protected $fillable = [
            'plugin_id', 'is_virtual', 'uid', 'uniacid', 'dispatch_type_id', 'mark',
            'goods_price', 'price', 'order_goods_price', 'vip_order_goods_price',
            'cost_amount', 'discount_price', 'fee_amount', 'service_fee_amount',
            'deduction_price', 'dispatch_price', 'goods_total', 'no_refund',
            'order_sn', 'create_time', 'note', 'shop_name', 'need_address',
            'invoice_type', 'rise_type', 'collect_name', 'company_number',
            'paytype', 'dkfee', 'mid', 'updated_at', 'created_at', 'status',
            // 补充SQL表中的其他字段
            'store_id', 'is_deleted', 'is_member_deleted', 'finish_time', 
            'pay_time', 'delivery_time', 'cancel_time', 'deleted_at',
            'cancel_pay_time', 'cancel_send_time', 'pay_type_id', 
            'refund_id', 'is_plugin', 'change_price', 'change_dispatch_price',
            'comment_status', 'order_pay_id', 'is_pending', 'invoice',
            'auto_receipt', 'is_all_send_goods', 'close_reason',
            'camilo_resources_coupon_type', 'camilo_resources_recharge_number'
        ];
    
    /**
     * 获取状态名称
     */
    public function getStatusNameAttribute()
    {
        $statusNames = [
            self::CLOSE => '已取消',
            self::WAIT_PAY => '待付款',
            self::WAIT_SEND => '待发货',
            self::WAIT_RECEIVE => '待收货',
            self::COMPLETE => '已完成',
            self::REFUND => '退款中'
        ];
        
        return $statusNames[$this->status] ?? '未知状态';
    }
    
    /**
     * 获取支付方式名称
     */
    public function getPayTypeNameAttribute()
    {
        if ($this->payType) {
            return $this->payType->name;
        }
        return '未知支付方式';
    }
    
    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }
    
    /**
     * 关联支付方式
     */
    public function payType()
    {
        return $this->belongsTo(PayType::class, 'pay_type_id', 'id');
    }
    
    /**
     * 关联订单商品
     */
    public function orderGoods()
    {
        return $this->hasMany(HhmOrderGoods::class, 'order_id', 'id');
    }
    
    /**
     * 关联门店
     */
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }
    
    // /**
    //  * 关联门店订单
    //  */
    // public function storeOrder()
    // {
    //     return $this->hasOne(StoreOrder::class, 'order_id', 'id');
    // }
    
    /**
     * 订单状态:待付款
     */
    public function scopeWaitPay($query)
    {
        return $query->where('status', self::WAIT_PAY);
    }
    
    /**
     * 订单状态:待发货
     */
    public function scopeWaitSend($query)
    {
        return $query->where('status', self::WAIT_SEND);
    }
    
    /**
     * 订单状态:待收货
     */
    public function scopeWaitReceive($query)
    {
        return $query->where('status', self::WAIT_RECEIVE);
    }
    
    /**
     * 订单状态:已完成
     */
    public function scopeCompleted($query)
    {
        return $query->where('status', self::COMPLETE);
    }
    
    /**
     * 订单状态:已取消
     */
    public function scopeCancelled($query)
    {
        return $query->where('status', self::CLOSE);
    }
    
    /**
     * 按插件ID筛选
     */
    public function scopePluginId($query, $pluginId)
    {
        return $query->where('plugin_id', $pluginId);
    }
    
    /**
     * 搜索订单
     */
    public function scopeSearch($query, $params)
    {
        if (!empty($params['order_sn'])) {
            $query->where('order_sn', 'like', '%' . $params['order_sn'] . '%');
        }
        
        if (!empty($params['status']) && $params['status'] !== '') {
            $query->where('status', $params['status']);
        }
        
        if (!empty($params['pay_type_id'])) {
            $query->where('pay_type_id', $params['pay_type_id']);
        }
        
        if (!empty($params['plugin_id'])) {
            $query->where('plugin_id', $params['plugin_id']);
        }
        
        return $query;
    }
    
    /**
     * 关联会员（用于审核系统）
     */
    public function member()
    {
        return $this->belongsTo(Member::class, 'uid', 'uid');
    }
    
    /**
     * 关联发货单（用于审核系统）
     */
    public function outbounds()
    {
        return $this->hasMany(OrderOutbound::class, 'order_id', 'id');
    }
}